package spingmvcstudent.contoller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import spingmvcstudent.pojo.R;
import spingmvcstudent.pojo.User;
import spingmvcstudent.service.UserServiceImpl;

import java.util.List;

@RestController
@RequestMapping("/user")
//@Component(value = "userController")
public class UserContoller {
    @Autowired
    UserServiceImpl userServiceimpl;

    @GetMapping("/list")
   public R list(){
        List<User> allUser = userServiceimpl.getAllUser();
        System.out.println(allUser);
        return R.ok(allUser);
    }

    @PostMapping("/add")
    public int add(@RequestBody User user){
        int i = userServiceimpl.addUser(user);
        System.out.println(i);
        return i;
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable(name = "id") Integer id){

        System.out.println("删除的用户id:" + id);
        int i = userServiceimpl.deleteUser(id);

        return R.ok(null);

    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(name = "id") Integer id){

        System.out.println("查询的用户id:" + id);
        List<User> byId = userServiceimpl.findById(id);

        return R.ok(byId);

    }

    @PutMapping("/update")
    public int update(@RequestBody User user){

        System.out.println("修改的用户id:" + user.getId());
        int i = userServiceimpl.updateUser(user);

        return i;

    }

    @PostMapping("/login")
    public String login(@RequestBody User user){

        System.out.println("登录的用户id:" + user.getId());
        String byNameAndPassword = userServiceimpl.findByNameAndPassword(user.getUsername(), user.getPassword());

        return byNameAndPassword;

    }
}
